﻿using Payroll.BusinessLogic.Devices;
using Payroll.BusinessLogic.Profile;
using Payroll.Common.Utils;
using Payroll.BusinessLogic.Configuration;
using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Payroll.Controls;

namespace PayrollHR.Administration.Modules
{
    public partial class OrderControl : BaseAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindGrid();
            }
        }
        
        protected List<Order> GetOrders()
        {
            DateTime? startDate = ctrlStartDatePicker.SelectedDate;
            DateTime? endDate = ctrlEndDatePicker.SelectedDate;
            if (startDate.HasValue)
            {
                startDate = DateTimeHelper.ConvertToUtcTime(startDate.Value, DateTimeHelper.CurrentTimeZone);
            }
            if (endDate.HasValue)
            {
                endDate = DateTimeHelper.ConvertToUtcTime(endDate.Value, DateTimeHelper.CurrentTimeZone).AddDays(1);
            }
            var orders = this.OrderService.GetAllOrder(startDate, endDate, null, 100, 0);
            return orders;
        }
        protected void BindGrid()
        {
            var orders = GetOrders();
            if (orders.Count > 0)
            {
                this.gvOrders.Visible = true;
                this.lblNoOrdersFound.Visible = false;
                this.gvOrders.DataSource = orders;
                this.gvOrders.DataBind();
            }
            else
            {
                this.gvOrders.Visible = false;
                this.lblNoOrdersFound.Visible = true;
            }
        }
        protected void BindGridDiscount()
        { 
        }
        protected void Delete()
        {
            if (Page.IsValid)
            { 
                string orderId = "";
                try
                {
                    foreach (GridViewRow row in gvOrders.Rows)
                    {
                        var cbOrder = row.FindControl("cbOrder") as CheckBox;
                        bool isChecked = cbOrder.Checked;
                        if (isChecked)
                        {
                            orderId += gvOrders.DataKeys[row.RowIndex].Value + ",";
                        }
                    }
                    if (orderId.Length > 0)
                    {
                        orderId = orderId.Substring(0, orderId.Length - 1);
                        this.OrderService.Delete(orderId);
                        this.BindGrid();
                    }
                }
                catch (Exception ex)
                {
                    ProcessException(ex);
                }
            }
        }
        protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvOrders.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    BindGrid();
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }

        protected void ExportButton_Click(object sender, EventArgs e)
        {

        }

        protected void FilterButton_Click(object sender, EventArgs e)
        {
            try
            {
                string registrationFrom;
                string registrationTo = dpToDate.SelectedDate.Value.ToString("yyyy-MM-dd");

                registrationFrom = string.Format("{0}-01", dpToDate.SelectedDate.Value.ToString("yyyy-MM"));
                string customername = txtCustomerName.Text.Trim();
                DataTable dt = new DataTable();
                SqlHelper.Warehouse(registrationFrom: registrationFrom, registrationTo: registrationTo, customerId: 0, custtomerName: customername, dt: ref dt);
                
                gvInputOut.DataSource = dt;
                gvInputOut.DataBind();
            }
            catch { }
        }

        protected void gvInputOut_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInputOut.PageIndex = e.NewPageIndex;

        }

        protected void gvInputOut_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblCol1 = e.Row.FindControl("lblCol2") as Label;
                Label lblCol2 = e.Row.FindControl("lblCol3") as Label;
                Label lblCol4 = e.Row.FindControl("lblCol4") as Label;
                if (lblCol1 != null && lblCol2 != null)
                {
                    int val1 = CommonHelper.ToInteger(lblCol1.Text);
                    int val2 = CommonHelper.ToInteger(lblCol2.Text);
                    lblCol4.Text = CommonHelper.ToString(val1 - val2);
                }
            }
        }
    }
}